package cn.iocoder.yudao.module.warehouseUsage.service.warehouseUsage;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.warehouseUsage.controller.admin.warehouseUsage.vo.*;
import cn.iocoder.yudao.module.warehouseUsage.dal.dataobject.warehouseUsage.warehouseUsageDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.warehouseUsage.dal.mysql.warehouseUsage.warehouseUsageMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.warehouseUsage.enums.ErrorCodeConstants.*;

/**
 * 使用记录 Service 实现类
 *
 * @author wwwhk
 */
@Service
@Validated
public class warehouseUsageServiceImpl implements warehouseUsageService {

    @Resource
    private warehouseUsageMapper warehouseUsageMapper;

    @Override
    public Long createwarehouseUsage(warehouseUsageSaveReqVO createReqVO) {
        // 插入
        warehouseUsageDO warehouseUsage = BeanUtils.toBean(createReqVO, warehouseUsageDO.class);
        warehouseUsageMapper.insert(warehouseUsage);

        // 返回
        return warehouseUsage.getId();
    }

    @Override
    public void updatewarehouseUsage(warehouseUsageSaveReqVO updateReqVO) {
        // 校验存在
        validatewarehouseUsageExists(updateReqVO.getId());
        // 更新
        warehouseUsageDO updateObj = BeanUtils.toBean(updateReqVO, warehouseUsageDO.class);
        warehouseUsageMapper.updateById(updateObj);
    }

    @Override
    public void deletewarehouseUsage(Long id) {
        // 校验存在
        validatewarehouseUsageExists(id);
        // 删除
        warehouseUsageMapper.deleteById(id);
    }

    @Override
        public void deletewarehouseUsageListByIds(List<Long> ids) {
        // 删除
        warehouseUsageMapper.deleteByIds(ids);
        }


    private void validatewarehouseUsageExists(Long id) {
        if (warehouseUsageMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_USAGE_NOT_EXISTS);
        }
    }

    @Override
    public warehouseUsageDO getwarehouseUsage(Long id) {
        return warehouseUsageMapper.selectById(id);
    }

    @Override
    public PageResult<warehouseUsageDO> getwarehouseUsagePage(warehouseUsagePageReqVO pageReqVO) {
        return warehouseUsageMapper.selectPage(pageReqVO);
    }

}